const {   
    Program,
    AnchorProvider,
    BN,
    web3,
    Wallet,
} = require("@coral-xyz/anchor");


const {  
    Connection,
    clusterApiUrl,
    PublicKey,
    Keypair
} = require('@solana/web3.js')

const idl = require("./idl/basic_1.json");

const programID = new PublicKey("9h1NwQbGyxdfix8q7Bp91Ss6eSiJG63CYZ8qR6ouBBpR");

const opts = {
    preflightCommitment: "recent",
  };

const config = require('./config');
const {user1, user2, user3} = require('./0_keypair');
const run = async () => {
    const network = clusterApiUrl("devnet");
    //const connection = new Connection(network, opts.preflightCommitment);
    const connection = new Connection("http://127.0.0.1:8899", opts.preflightCommitment);
    
    //const user1Keypair = await user1();

    const keypairfile = require('/home/hlf/sol-dev/id.json');
    const user1Keypair =  Keypair.fromSecretKey(Uint8Array.from(keypairfile));


    const user3_keypair = await user3();

    const wallet = new Wallet(user1Keypair);
    const provider = new AnchorProvider(
      connection, wallet, opts.preflightCommitment,
    )
    
    const program = new Program(idl, programID, provider);
    const myAccount = web3.Keypair.generate();
    let txSignature = await program.methods.initialize(new BN(1234)).accounts({
            myAccount: myAccount.publicKey,
            user: provider.wallet.publicKey,
            systemProgram: web3.SystemProgram.programId,}).signers([myAccount]).rpc();

    console.log('txSignature: ', txSignature);

    const acc = await program.account.myAccount.fetch(myAccount.publicKey);
    console.log('acc: ', acc)

}

run();